package servlet;

import java.io.*;
import java.util.*;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import mgr.ProductoManager;
import to.ProductoTO;

@SuppressWarnings("serial")
public class BuscarProductoAction extends HttpServlet {
    
    public void doGet(HttpServletRequest req, HttpServletResponse resp)
        throws ServletException, IOException {
        procesar(req, resp);
    }
    
    public void doPost(HttpServletRequest req, HttpServletResponse resp)
        throws ServletException, IOException {
        procesar(req, resp);
    }

    private void procesar(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        String cProducto = req.getParameter("cProducto");
        String dProducto = req.getParameter("dProducto");
        
        try {
            
            List<ProductoTO> listadoProductos;
            ProductoTO prod_to = new ProductoTO(cProducto, "", "", dProducto, "", 0, 0, 0, "", "", "", "", true);
            ProductoManager productos = new ProductoManager();
            listadoProductos = productos.filtraProducto(prod_to, "0001");
            
            HttpSession sesion = req.getSession();
            sesion.setAttribute("listadoProductos", listadoProductos);
            
            getServletContext().getRequestDispatcher("/Listado_Productos.jsp").forward(req, resp);
            
        }
        catch (Exception e) {
            PrintWriter out = resp.getWriter();
            out.println("<HTML>");
            out.println("<HEAD>");
            out.println("</HEAD>");
            out.println("<BODY>");

            String msg = (String) e.getMessage();
            out.println("<P>" + msg + "</P>");

            out.println("</BODY>");
            out.println("</HTML>");
	}
    }
}